package com.yinhe.system.service.impl;

import com.yinhe.common.annotation.DataScope;
import com.yinhe.common.core.domain.entity.SysRole;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.utils.DateUtils;
import com.yinhe.system.domain.SysUserRole;
import com.yinhe.system.domain.YhLeave;
import com.yinhe.system.mapper.SysRoleMapper;
import com.yinhe.system.mapper.SysUserMapper;
import com.yinhe.system.mapper.YhLeaveMapper;
import com.yinhe.system.service.IYhLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 休假记录Service业务层处理
 * 
 * @author yinhe
 * @date 2022-07-26
 */
@Service
public class YhLeaveServiceImpl implements IYhLeaveService 
{
    @Autowired
    private YhLeaveMapper yhLeaveMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询休假记录
     * 
     * @param id 休假记录主键
     * @return 休假记录
     */
    @Override
    public YhLeave selectYhLeaveById(Long id)
    {
        return yhLeaveMapper.selectYhLeaveById(id);
    }

    /**
     * 查询休假记录列表
     * 
     * @param yhLeave 休假记录
     * @return 休假记录
     */
    @Override
    @DataScope(deptAlias = "L", userAlias = "L")
    public List<YhLeave> selectYhLeaveList(YhLeave yhLeave)
    {
        return yhLeaveMapper.selectYhLeaveList(yhLeave);
    }

    /**
     * 局长政委查询休假记录列表
     *
     * @param yhLeave 休假记录
     * @return 休假记录
     */
    @Override
    @DataScope(deptAlias = "L", userAlias = "L")
    public List<YhLeave> selectYhList(YhLeave yhLeave)
    {
        return yhLeaveMapper.selectYhList(yhLeave);
    }

    /**
     * 新增休假记录
     * 
     * @param yhLeave 休假记录
     * @return 结果
     */
    @Override
    public int insertYhLeave(YhLeave yhLeave)
    {
        yhLeave.setCreateTime(DateUtils.getNowDate());
        return yhLeaveMapper.insertYhLeave(yhLeave);
    }

    /**
     * 修改休假记录
     * 
     * @param yhLeave 休假记录
     * @return 结果
     */
    @Override
    public int updateYhLeave(YhLeave yhLeave)
    {
        yhLeave.setUpdateTime(DateUtils.getNowDate());
        return yhLeaveMapper.updateYhLeave(yhLeave);
    }
    /**
     * 销假记录
     *
     * @param yhLeave 休假记录
     * @return 结果
     */
    @Override
    public int backYhLeave(YhLeave yhLeave)
    {
        yhLeave.setUpdateTime(DateUtils.getNowDate());
        return yhLeaveMapper.backYhLeave(yhLeave);
    }

    /**
     * 审核休假记录
     *
     * @param yhLeave 休假记录
     * @return 结果
     */
    @Override
    public int viewYhLeave(YhLeave yhLeave)
    {
        yhLeave.setUpdateTime(DateUtils.getNowDate());
        return yhLeaveMapper.viewYhLeave(yhLeave);
    }

    /**
     * 局长审核休假记录
     *
     * @param yhLeave 休假记录
     * @return 结果
     */
    @Override
    public int chiefYhLeave(YhLeave yhLeave)
    {
        yhLeave.setUpdateTime(DateUtils.getNowDate());
        return yhLeaveMapper.chiefYhLeave(yhLeave);
    }

/**
 * 政委审核休假记录
 *
 * @param yhLeave 休假记录
 * @return 结果
 */
    @Override
    public int gaottYhLeave(YhLeave yhLeave)
    {
        yhLeave.setUpdateTime(DateUtils.getNowDate());
        return yhLeaveMapper.gaottYhLeave(yhLeave);
    }
    /**
     * 批量删除休假记录
     * 
     * @param ids 需要删除的休假记录主键
     * @return 结果
     */
    @Override
    public int deleteYhLeaveByIds(Long[] ids)
    {
        return yhLeaveMapper.deleteYhLeaveByIds(ids);
    }

    /**
     * 删除休假记录信息
     * 
     * @param id 休假记录主键
     * @return 结果
     */
    @Override
    public int deleteYhLeaveById(Long id)
    {
        return yhLeaveMapper.deleteYhLeaveById(id);
    }

    /**
     * 查询用户Id,根据用户Id查询角色
     *
     * @param userId 用户Id
     * @return 结果
     */
    @Override
    public SysRole selectRolekey(Long userId) {
        //获取roleId
        SysUserRole userRole = sysRoleMapper.selectRolekey(userId);
        userRole.getRoleId();
        return  sysRoleMapper.selectRoleByRoleId(userRole.getRoleId());

    }

    /**
     * 查询待审人员部门领导
     *
     * @param deptId 用户Id
     * @return 结果
     */
    @Override
    public List<SysUser> selectUserByDeptId(Long deptId) {
        ArrayList<SysUser> result = new ArrayList<SysUser>();
        List<SysUser>  deptUser = sysUserMapper.selectUserByDeptId(deptId);
        //从for中获取部门id相等的用户
        for (SysUser a:deptUser) {
            //根据userid来查sysRole中RoleKey为2001的，然后userid和roleid相等，根据roleid来查userid那条的电话号
            List<SysUserRole> userRoleList = sysRoleMapper.selectRolekeyList(a.getUserId());//获取roleId
            for (SysUserRole c : userRoleList) {
                List<SysRole> sysRole = sysRoleMapper.selectByRoleId(c.getRoleId());//查询roleId数据
                for (SysRole b : sysRole) {
                    if (b.getRoleKey().substring(0, 4).equals("2001")) {
                        b.getRoleId();
                        List<SysUserRole> user = sysRoleMapper.selectUserIdList(b.getRoleId());//通过roleId查询userId
                        for(SysUserRole d : user){
                            List<SysUser> userList=sysUserMapper.selectUserByUserId(d.getUserId());
                            for(SysUser e :userList){
                                if(e.getDeptId()==deptId){
                                    SysUser sysUser = sysUserMapper.selectUserById(e.getUserId());
                                    //System.out.println(sysUser+"用户对象");
                                    result.add(sysUser);
                                }
                            }
                        }
                    }
                }
            }
        }
         return result;
    }
    /**
     * 查询局长待审
     *
     * @param
     * @return 结果
     */
    @Override
    public SysUser selectDirector() {
     SysRole sysRole = sysRoleMapper.selectDirector();
     SysUserRole user = sysRoleMapper.selectUserId(sysRole.getRoleId());//通过roleId查询userId
     SysUser sysUser = sysUserMapper.selectUserById(user.getUserId());
     return sysUser ;
    }
    /**
     * 查询政委待审
     *
     * @param
     * @return 结果
     */
    @Override
    public SysUser selectCommissor() {
        SysRole sysRole = sysRoleMapper.selectCommissor();
        SysUserRole user = sysRoleMapper.selectUserId(sysRole.getRoleId());//通过roleId查询userId
        SysUser sysUser = sysUserMapper.selectUserById(user.getUserId());
        return sysUser ;
    }
}
